package com.android.hdhe.uhf.main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.android.hdhe.uhf.reader.Tools;
import com.android.hdhe.uhf.reader.UhfReader;
import com.ljsw.activity.R;

public class MainActivity extends Activity implements OnClickListener{
	private Button buttonClear;
	private Button buttonConnect;
	private Button buttonStart;
	private ListView listViewData;
	private Button buttonMore ;
	private ArrayList<EPC> listEPC;
	private ArrayList<Map<String, Object>> listMap;
	private boolean runFlag = true;
	private boolean startFlag = false;
	private boolean connectFlag = false;
	private UhfReader reader ; //超高频读写器 
	
	private MyApplication myAppli ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		myAppli = (MyApplication) getApplication();
		initView();
		//获取读写器实例，若返回Null,则串口初始化失败
		reader = UhfReader.getInstance();
		if(reader == null){
			Toast.makeText(this, "serialport init fail", 0).show();
			setButtonClickable(buttonClear, false);
			setButtonClickable(buttonStart, false);
			setButtonClickable(buttonConnect, false);
			return ;
		}
		Util.initSoundPool(this);
		Thread thread = new InventoryThread();
		thread.start();
	}

	private void initView(){
//		buttonStart = (Button) findViewById(R.id.button_start);
//		buttonConnect = (Button) findViewById(R.id.button_connect);
//		buttonClear = (Button) findViewById(R.id.button_clear);
//		listViewData = (ListView) findViewById(R.id.listView_data);
//		buttonMore = (Button) findViewById(R.id.button_more_operation);
		buttonMore.setOnClickListener(this);
		buttonStart.setOnClickListener(this);
		buttonConnect.setOnClickListener(this);
		buttonClear.setOnClickListener(this);
		setButtonClickable(buttonStart, false);
		setButtonClickable(buttonMore, false);
		listEPC = new ArrayList<EPC>();
		
	}
	
	
	@Override
	protected void onPause() {
		startFlag = false;
		super.onPause();
	}
	
	@Override
	protected void onDestroy() {
		if(reader != null){
			reader.close();
		}
		super.onDestroy();
	}
	/**
	 * 盘存线程
	 * @author Administrator
	 *
	 */
	class InventoryThread extends Thread{
		private List<byte[]> epcList;

		@Override
		public void run() {
			super.run();
			while(runFlag){
				
				if(startFlag){
//					reader.stopInventoryMulti()
					epcList = reader.inventoryRealTime(); //实时盘存
					if(epcList != null && !epcList.isEmpty()){
						//播放提示音
						Util.play(1, 0);
						for(byte[] epc:epcList){
							String epcStr = Tools.Bytes2HexString(epc, epc.length);
//							String epcStr = new String(epc);
							addToList(listEPC, epcStr);
						}
					}
					epcList = null ;
					try {
						Thread.sleep(20);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
	}
		
	
	//设置按钮是否可用
	private void setButtonClickable(Button button, boolean flag){
		button.setClickable(flag);
		if(flag){
			button.setTextColor(Color.BLACK);
		}else{
			button.setTextColor(Color.GRAY);
		}
	}
	
	//将读取的EPC添加到LISTVIEW
	private void addToList(final List<EPC> list, final String epc){
		runOnUiThread(new Runnable() {
			
			public void run() {
				
				//第一次读入数据
				if(list.isEmpty()){
					EPC epcTag = new EPC();
					epcTag.setEpc(epc);
					epcTag.setCount(1);
					list.add(epcTag);
					//添加到application
					myAppli.addEPC(epc);
				}else{
					for(int i = 0; i < list.size(); i++){
						EPC mEPC = list.get(i);
						//list中有此EPC
						if(epc.equals(mEPC.getEpc())){
						mEPC.setCount(mEPC.getCount() + 1);
						list.set(i, mEPC);
						break;
					}else if(i == (list.size() - 1)){
						//list中没有此epc
						EPC newEPC = new EPC();
						newEPC.setEpc(epc);
						newEPC.setCount(1);
						list.add(newEPC);
						//添加到application
						myAppli.addEPC(epc);
						}
					}
				}
				//将数据添加到ListView
				listMap = new ArrayList<Map<String,Object>>();
				int idcount = 1;
				for(EPC epcdata:list){
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("ID", idcount);
					map.put("EPC", epcdata.getEpc());
					map.put("COUNT", epcdata.getCount());
					
					idcount++;
					listMap.add(map);
				}
//				listViewData.setAdapter(new SimpleAdapter(MainActivity.this,
//						listMap, R.layout.listview_item, 
//						new String[]{"ID", "EPC", "COUNT"}, 
//						new int[]{R.id.textView_id, R.id.textView_epc, R.id.textView_count}));
			}
		});
	}

	/**
	 * 清空listview
	 */
	private void clearData(){
		listEPC.removeAll(listEPC);
		listViewData.setAdapter(null);
		myAppli.getListEpc().removeAll(myAppli.getListEpc());
	}

	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}
	
//	@Override
//	public void onClick(View v) {
//
//		switch (v.getId()) {
//		case R.id.button_start:
//			if(!startFlag){
//				startFlag = true ;
//				buttonStart.setText(R.string.stop_inventory);
//			}else{
//				startFlag = false;
//				buttonStart.setText(R.string.inventory);
//			}
//			break;
//		case R.id.button_connect:
//			
//			byte[] versionBytes = reader.getFirmware();
//			if(versionBytes != null){
////				reader.setWorkArea(3);//设置成欧标
//				Util.play(1, 0);
//				String version = new String(versionBytes);
////				textVersion.setText(new String(versionBytes));
//				setButtonClickable(buttonConnect, false);
//				setButtonClickable(buttonStart, true);
//				setButtonClickable(buttonMore, true);
//				return;
//			}
//			Toast.makeText(getApplicationContext(), "连接模块失败", 0).show();
//			break;
//		case R.id.button_more_operation:
//			if(myAppli.getListEpc() == null || myAppli.getListEpc().isEmpty()){
//				Toast.makeText(getApplicationContext(), "请先盘存再进行操作", 0).show();
//				return;
//			}
//			Intent intent = new Intent(MainActivity.this, MoreOperationActivity.class);
//			startActivity(intent);
//			break;
//		case R.id.button_clear:
//			clearData();
//			break;
//		default:
//			break;
//		}
//		
//	}
	
}
